// ActionScript file
			import classes.AppConfig;
			import classes.HistoryObject;
			
			import components.MainObject;
			import components.MoveResizeMark;
			
			import flash.events.Event;
			import flash.events.MouseEvent;
			import flash.geom.Rectangle;
			import flash.ui.Keyboard;
			import flash.utils.setTimeout;
			
			import mx.containers.Canvas;
			import mx.controls.Alert;
			import mx.controls.CheckBox;
			import mx.events.ColorPickerEvent;
			import mx.managers.CursorManager;
			
			import org.flexlancer.Logger.Logger;
			
			[Bindable]
            [Embed(source="assets/HandCursor.png")]
			private var HandIcon:Class;
            
            [Bindable]
            private var currentPage:Number = 1;
            private var selectedMark:MoveResizeMark;
			private var imgsXML:XML;
			private var ctrls:Array;
			private var ValidEntry:Number=100;
			private var totalImages:Number = 0;
			private var lastZoomValue:Number=1;
			private var cursorID:Number;
			private var mouseDownFlag:Boolean = false;
			private var fileToLoad:String = "";
			private var undoFlag:Boolean = false;
			private var History:Array;
			private var commitStatus:Array = null;
            
			private function OnCC():void{
				try{
					var ldr:URLLoader = new URLLoader();
					this.MoverStage.addEventListener(MouseEvent.MOUSE_DOWN,this.OnImageMouseDown);
					this.MoverStage.addEventListener(MouseEvent.MOUSE_UP,this.OnImageMouseUp);
					this.MoverStage.addEventListener(MouseEvent.MOUSE_MOVE,this.OnImageMouseMove);
					this.MoverStage.addEventListener(MouseEvent.MOUSE_OUT,this.OnImageMouseUp);
					this.addEventListener(KeyboardEvent.KEY_DOWN,this.MoverStage_OnKeyDown);
					this.History = new Array();
					ldr.addEventListener(Event.COMPLETE,OnDataLoad);
					
					//this.fileToLoad = Application.application.parameters.dFile;
					//AppConfig.LOGIN_USER = Application.application.parameters.currUser;
					
					this.fileToLoad = "data.xml";
					AppConfig.LOGIN_USER = "shahid"; 
					
					if ( this.fileToLoad != null && this.fileToLoad != "" ){
						ldr.load(new URLRequest(this.fileToLoad));
					}
					this.MoverStage.setFocus();
					
					
			 	}catch(err:Error){
					Logger.LogE(err);
				}//catch
			}//OnCC ENDs
		
			public function SelectMark(mark:MoveResizeMark):void{
				try{
					if ( this.selectedMark != null ){
						this.selectedMark.Selected = false;
					}
					this.selectedMark = mark;
					this.selectedMark.Selected = true;
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			public function get Rotate():Number{
					var rotate:Number = MainObject(this.ctrls[this.currentPage-1])._rotation;
					while ( rotate < 0 ) rotate += 360;
					rotate %= 360;
					return rotate;
			}
			public function set DT(val:String):void{
				if ( this.dp != null )
				this.dp.Value = val;
			}
			public function GetContainerRectangle():Rectangle{
				try{
					var vWidth:Number = Canvas(this.ctrls[this.currentPage-1]).width;
					var vHeight:Number = Canvas(this.ctrls[this.currentPage-1]).height;
					if ( this.Rotate == 90 || this.Rotate == 270 )
					{
						vWidth =  Canvas(this.ctrls[this.currentPage-1]).height;
						vHeight =  Canvas(this.ctrls[this.currentPage-1]).width;
					}
					
					vWidth /= MainObject(this.ctrls[this.currentPage-1])._scale;
					vHeight /=  MainObject(this.ctrls[this.currentPage-1])._scale;
					
					//Alert.show(Canvas(this.ctrls[this.currentPage-1]).measuredWidth.toString());
					
					var rec:Rectangle = new Rectangle(0,0,vWidth,vHeight);
					//MoverStage.graphics.lineStyle(5,0x000000,1);
					//MoverStage.graphics.drawRect(vX,vY,vWidth,vHeight);
					this.dp.Value = "["+rec.width+","+rec.height+", "+this.Rotate.toString()+"]";
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
				return rec;
			}
			
			private function MoverStage_OnKeyDown(evt:KeyboardEvent):void{
				try{
					if ( evt.keyCode == Keyboard.UP ){
						if ( this.MoverStage.maxVerticalScrollPosition == 0 ){
							this.OnPageUp();
						}
					}
					if ( evt.keyCode == Keyboard.DOWN ){
						if ( this.MoverStage.maxVerticalScrollPosition == 0 ){
							this.OnPageDown();
						}
					}
					if ( evt.keyCode == Keyboard.DELETE ){
						this.OnDelMark();
					}
					//Alert.show(evt.keyCode.toString(),"Key code");
			 	}catch(err:Error){
					Logger.LogE(err);
				}//catch
			} 
			
			private function OnSliderChange():void{
				try{
					this.txtZoom.text = this.sliderZoom.value.toString()+"%";
					this.ValidEntry = this.sliderZoom.value;
					this.ZoomObjects(this.ValidEntry/100);
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
			}//OnSliderChange ENDs
			
			private function OnAlphaSliderChange():void{
				try{
					if(this.selectedMark != null){
						var valAlpha:Number = Number(this.sliderAlpha.value/100);
						this.selectedMark.alpha = valAlpha;
						//var valAlpha:Number = Number(this.sliderAlpha.value/100);
						//this.ctrls[this.currentPage-1].setAlpha(valAlpha);
						
						////////shahid code
						
						/* var undoAlpha:String = (valAlpha).toString();
						
						if (undoAlpha){
							var props:XML = new XML(<root />);
							props.UndoAlpha = undoAlpha;
							this.RecordHistory("UNDOALPHA",props);
						} */
					}
					}catch(err:Error){
						Logger.LogE(err);
				}//catch
			}
			
			private function OnAlphaSliderPress():void{
				try{
						var undoAlpha:String;
						if(this.selectedMark){
							undoAlpha = this.selectedMark.alpha.toString();
						}
						
						
						if (undoAlpha){
							var props:XML = new XML(<root />);
							props.UndoAlpha = undoAlpha;
							//var RotationClockWise:XMLList= 
							this.RecordHistory("UNDOALPHA",props);
						
						///////////--------
						}
					
					}catch(err:Error){
					Logger.LogE(err);
				}//catch
			}
			
			private function MakeCommitGloballyCheckBoxes(parmCount:Number):void{
				try{
					this.commitStatus = new Array();
					for ( var index:Number = 0 ; index < parmCount ; index ++){
						this.commitStatus.push(false);
					}
					/*for (var index:Number = 1; index <= parmCount ; index ++){
						var chk:CheckBox = new CheckBox();
						chk.label = "Page "+index.toString();
						chk.selected = false;
						chk.name = "chk"+index.toString();
						chk.addEventListener(Event.CHANGE,OnCheckBox_CommitChange);
						this.vboxCommitGlobally.addChild(chk);
					}*/
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function myDataTipFunc(val:String):String {
                return "Zoom: " + String(val.split(".")[0])+"%";
            //myDataTipFunc ENDs
            }
            
            private function myAlphaDataTipFunc(val:String):String {
                return "Alpha: " + String(val.split(".")[0])+"%";
            //myDataTipFunc ENDs
            }
            
            private function OnZoomEnterPress():void{
				try{
					var strVal:String = this.txtZoom.text.toString();
					var toks:Array = strVal.split("%");
					
					if ( toks[0] == "" ){
						this.sliderZoom.value = this.ValidEntry;
					}else{
						this.sliderZoom.value = Number(toks[0]);
						this.ValidEntry = Number(toks[0]);
					}
					if ( this.ValidEntry > 999 ){
						this.ValidEntry = 999;
					}
					if ( this.ValidEntry < 10 ){
						this.ValidEntry = 10;
					}
					this.txtZoom.text = this.ValidEntry.toString() + "%";
					this.ZoomObjects(this.ValidEntry/100);
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
			//OnZoomEnterPress ENDs	
			}
			
			private function OnLockZoom():void{
				try{
					
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			public function ChangeStatus():void {
					try{			
						this.sliderAlpha.value = Number((this.selectedMark.alpha)*100);
						this.cp.selectedColor = this.selectedMark.getStyle("backgroundColor");	
					
					
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
			}
			
			private function OnDataLoad(evt:Event):void{
				try{			
					this.imgsXML = XML(evt.target.data);
					//Alert.show(this.imgsXML.toXMLString());
					this.lblTotalPages.text = "/" + this.imgsXML.children().length().toString();
					this.currentPage = Number(this.imgsXML.@startuppage);
					//this.currentPage = Number(this.imgsXML.image.@instanceid);
					LoadImage();
					
					MakeCommitGloballyCheckBoxes(this.imgsXML.children().length());
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
			//OnDataLoad ENDs
			}
			
			private function SetData(parm:MainObject,pData:XML ):void{
				parm.data = pData;
			}
			
			private function LoadImage():void{
				try{
					var index:Number = 0;
					this.totalImages = this.imgsXML.children().length();
					this.ctrls = new Array();
					
					for each ( var singleNode:XML in this.imgsXML.children() ){
						var imgCtrl:MainObject= new MainObject();
						//imgCtrl.data = singleNode;
						setTimeout(this.SetData,1000,imgCtrl,singleNode.copy());
						imgCtrl.id = "image"+index.toString();
						imgCtrl.setStyle("horizontalCenter",0);
						imgCtrl.setStyle("top",0);
						this.ctrls.push(imgCtrl);
						index ++;
					}//LoadImage ENDs
					
					this.ctrls[this.currentPage-1].addEventListener("ImageLoaded",OnChildCreationComplete);
					this.MoverStage.addChild(this.ctrls[this.currentPage-1]);
					//setTimeout(this.OnFitPage,1400);
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
			//LoadImage ENDs	
			}
			
			private function OnImageMouseDown(evt:MouseEvent):void{
				this.cursorID = CursorManager.setCursor(this.HandIcon);
				this.mouseDownFlag = true;
				
//bm
				if(this.selectedMark){
					this.selectedMark.Selected = false;
					this.selectedMark = null;
				}  
			//OnImageMouseDown ENDs
			}
			
			private function OnImageMouseUp(evt:MouseEvent):void{
				CursorManager.removeCursor(this.cursorID);
				this.mouseDownFlag = false;
				/* if(this.selectedMark != null){
					this.selectedMark.stopDrag();
				} */
			}//OnImageMouseUP ENDs
			
			private var dx:Number = -100000;
			private var dy:Number = -100000;
			
			private function OnImageMouseMove(evt:MouseEvent):void{
				try{
					if ( this.ctrls == null ) return;
					if ( this.ctrls[this.currentPage-1] == null ) return;
					if ( this.ctrls[this.currentPage-1].isAnyMarkMove()){
						return;
					}
					
					if ( this.ctrls[this.currentPage-1].CommentController.isAnyCommentMove()){
						return;
					}
					if ( !this.mouseDownFlag ){
						dx = -100000;
						dy = -100000;
						return;					
					}//if
					if ( this.dx == -100000 ){
						this.dx = this.mouseX;
					}else{
						var mx:Number = -(this.mouseX - this.dx);
						if ( mx < 0 ){
							if ( this.MoverStage.horizontalScrollPosition + mx < 0 ){
								this.MoverStage.horizontalScrollPosition = 0;
							}else{
								this.MoverStage.horizontalScrollPosition += mx ;
							}//else
						}else{
							if ( this.MoverStage.horizontalScrollPosition + mx > this.MoverStage.maxHorizontalScrollPosition ){
								this.MoverStage.horizontalScrollPosition = this.MoverStage.maxHorizontalScrollPosition;
							}else{
								this.MoverStage.horizontalScrollPosition += mx;
							}//else
						}//else
					}//else
					
					if ( this.dy == -100000 ){
						this.dy = this.mouseY;
					}else{
						var my:Number = -(this.mouseY - this.dy);
						if ( my < 0 ){
							if ( this.MoverStage.verticalScrollPosition + my < 0 ){
								this.MoverStage.verticalScrollPosition = 0;
							}else{
								this.MoverStage.verticalScrollPosition += my ;
							}//else
						}else{
							if ( this.MoverStage.verticalScrollPosition + my > this.MoverStage.maxVerticalScrollPosition ){
								this.MoverStage.verticalScrollPosition = this.MoverStage.maxVerticalScrollPosition;
							}else{
								this.MoverStage.verticalScrollPosition += my;
							}//else
						}//else					
					}//else
					this.dx = this.mouseX;
					this.dy = this.mouseY;
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnAddMark(n:String = "", x:Number = -1,y:Number = -1, w:Number = 200, h:Number = 200, a:Number = -1, c:Number = -1):void{
				try{
					var mark:MoveResizeMark = new MoveResizeMark();
					if ( x == -1 ){
						mark.x = this.MoverStage.horizontalScrollPosition/this.lastZoomValue;
					}else{
						mark.x = x;
					}
					if ( y == -1 ){
						mark.y = this.MoverStage.verticalScrollPosition/this.lastZoomValue;
					}else{
						mark.y = y;
					}
					mark.name = n;
					var markBound:Rectangle = this.GetContainerRectangle();
					if (markBound.width < w){
						mark.width = markBound.width/2;	
					}else
					{
						mark.width = w;
					}
					if (markBound.height < h){
						mark.height = markBound.height/2;	
					}else
					{
						mark.height = h;
					}
					if ( a == -1 ){
						mark.alpha = 1;
						this.sliderAlpha.value = 100;
					}else{
						mark.alpha = a;
						this.sliderAlpha.value = a*100;
					}

					var markName:String = this.ctrls[this.currentPage-1].AddMark(mark);
					if ( c == -1 ){
						mark.setStyle("backgroundColor",0x000000);
						this.cp.selectedColor = 0x000000;
					}else{
						mark.setStyle("backgroundColor",c);
					}
					if ( markName != "" ){
						var props:XML = new XML(<root />);
						props.MarkName = markName;
						this.RecordHistory("RemoveMark",props);
					}
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnDelMark():void{
				try{
					if(this.selectedMark != null)
						this.ctrls[this.currentPage-1].RemoveMark(this.selectedMark.name.toString());
				}catch(err:Error){
					Logger.LogE(err);
				}
			}

			private function OnGotoPage():void{
				try{
					if ( this.txtPageNo.text == "" ){
						return;
					}
					var pg:Number = Number(this.txtPageNo.text);
					
					if ( pg > this.imgsXML.children().length() ){
						pg = this.imgsXML.children().length();
					}
					if ( pg < 1 ){
						pg = 1;
					}
					var props:XML = new XML(<root />);
					props.pageno = this.currentPage;
					
					this.MoverStage.removeChild(this.ctrls[this.currentPage-1]);
					this.currentPage = pg;
					
					if ( this.ctrls[this.currentPage-1].imageLoaded ){
						setTimeout(this.OnFitPage,300);
					}
					
					this.ctrls[this.currentPage-1].addEventListener("ImageLoaded",OnChildCreationComplete);
					this.MoverStage.addChild(this.ctrls[this.currentPage-1]);
					
					//var chkCommit:CheckBox = this.vboxCommitGlobally.getChildByName("chk"+this.currentPage.toString()) as CheckBox;
					this.ctrls[this.currentPage-1].CommitFlag = this.commitStatus[this.currentPage-1];
					this.chkCommitGlobally.selected = this.commitStatus[this.currentPage-1]
					this.RecordHistory("GotoPage",props);
					setTimeout(AdjustImageWRTCenter,100);
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
			//OnGotoPage ENDs
			}
			private function OnChildCreationComplete(evt:Event):void{
				try{
					evt.target.removeEventListener("ImageLoaded",OnChildCreationComplete);
					//Alert.show("Image Loaded");
					setTimeout(this.OnFitPage,300);
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			private function OnCheckBox_CommitChange(evt:Event):void{
				try{
					var chk:CheckBox = evt.target as CheckBox;
					/*if ( chk.name == "chk"+this.currentPage.toString() ){
						this.ctrls[this.currentPage-1].CommitFlag = chk.selected;
					}*/
					this.commitStatus[this.currentPage-1] = chk.selected;
					this.ctrls[this.currentPage-1].CommitFlag = chk.selected;
					if ( this.undoFlag ){
						return;
					}
					var props:XML = new XML(<root />);
					props.chkName = chk.name;
					props.chkValue = (!chk.selected).toString();
					this.RecordHistory("CommitChange",props);
					
				}catch(err:Error){
					Logger.LogE(err);
				}
			}

			public function AdjustImageWRTCenter():void{
				try{
					/*var biggerWidth:Number = this.MoverStage.width;
					var diffx:Number = 0;
					
					if ( biggerWidth > this.ctrls[this.currentPage-1].width ){
						 diffx = biggerWidth - this.ctrls[this.currentPage-1].width ;
						 diffx /= 2;
					}else{
						 diffx = 0 ;
						 this.MoverStage.horizontalScrollPosition = this.MoverStage.maxHorizontalScrollPosition/2;
					}
				 	this.ctrls[this.currentPage-1].x = diffx ;
				 	this.ctrls[this.currentPage-1].alpha = 1.0 ;
				 	*/
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			private var lastHscrollPos:Number = 0;
			private var lastVscrollPos:Number = 0;

			private function AdjustScrolls():void{
				try{
					this.AdjustImageWRTCenter();
					if ( this.MoverStage.maxHorizontalScrollPosition > 0 ){
						this.MoverStage.horizontalScrollPosition = this.lastHscrollPos * this.MoverStage.maxHorizontalScrollPosition;
					}

					if ( this.MoverStage.maxVerticalScrollPosition > 0 ){
						this.MoverStage.verticalScrollPosition = this.lastVscrollPos * this.MoverStage.maxVerticalScrollPosition;
					}						
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
			}
			private function ZoomObjects(val:Number):void{
				try{
					/* if ( this.MoverStage.maxHorizontalScrollPosition > 0 ){
						this.lastHscrollPos = this.MoverStage.horizontalScrollPosition / this.MoverStage.maxHorizontalScrollPosition;
					}
					if ( this.MoverStage.maxVerticalScrollPosition > 0 ){
						this.lastVscrollPos = this.MoverStage.verticalScrollPosition / this.MoverStage.maxVerticalScrollPosition;
					} 
					*/
					for each ( var sImage:MainObject in this.ctrls ){
						
						//sImage.scaleY = sImage.scaleX = val;
						sImage.Scale = val;
						//sImage.OnResize();
					}
					this.lastZoomValue = val;
					/*
					if ( this.MoverStage.maxHorizontalScrollPosition == 0 && this.MoverStage.maxVerticalScrollPosition == 0 ){
						this.callLater(AdjustImageWRTCenter);
					}else{
						this.callLater(AdjustScrolls);
					}*/
				}catch(err:Error){
					Logger.LogE(err);
				}//catch
			}//zoomObjects ENDs

			public function RecordHistory(parm:String,parmProps:XML):void{
				try{
					if ( this.undoFlag ){
						return;
					}
					var objInsert:HistoryObject = new HistoryObject(parm,this.currentPage,parmProps);
					this.History.push(objInsert);
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
		
			/* private function startAllDraging():void{
				for each ( var sImage:SWFLoader in this.ctrls ){
					sImage.startDrag();
				}
			}
			private function stopAllDraging():void{
				for each ( var sImage:SWFLoader in this.ctrls ){
					sImage.stopDrag();
				}
			} */
			/*
			private function OnFitPage():void{
				try{
					var zoomFactor:Number = 0;
					var aw:Number = this.ctrls[this.currentPage-1].height; 
					var preferChange:Number = (this.MoverStage.height - 19) / aw; 
					
					if ( this.ctrls[this.currentPage-1].width > this.ctrls[this.currentPage-1].height ){
						preferChange = (this.MoverStage.width - 20) / this.ctrls[this.currentPage-1].width;
					}
					zoomFactor = this.lastZoomValue * preferChange;
					this.txtZoom.text = (zoomFactor * 100 ).toString();
					this.OnZoomEnterPress();
					//this.AdjustImageWRTCenter();
				}catch(err:Error){
					
					Logger.LogE(err);
				}
			}*/
			private function OnFitPage():void{
				try{
					var zoomFactor:Number = 0;
					var MS_Width:Number = this.MoverStage.width;
					var MS_Height:Number = this.MoverStage.height - 20;
					var IMG_Width:Number = this.ctrls[this.currentPage - 1].width / this.lastZoomValue;
					var IMG_Height:Number = this.ctrls[this.currentPage - 1].height / this.lastZoomValue;
					var newScaleX:Number = MS_Width / IMG_Width ; 
					var newScaleY:Number = MS_Height / IMG_Height ;
					
					var preferChange:Number = newScaleX > newScaleY ? newScaleY : newScaleX;
					/*
					var case_no:Number = 0;
					
					if ( MS_Width < IMG_Width )
					{
						if ( MS_Height < IMG_Height )
						{
							case_no = 3;
						}
						else
						{
							case_no = 2;
						}
					}
					else if ( MS_Height < IMG_Height ) {
						case_no = 1;
					}else{
						case_no = 3
					}
					
					switch(case_no){
						case 1:
						preferChange = MS_Height / IMG_Height;
						break;
						case 2:
						preferChange = MS_Width/IMG_Width;
						break;
						case 3:
						if ( IMG_Width > IMG_Height ) preferChange = MS_Width / IMG_Width;
						else preferChange = MS_Height / IMG_Height;
						break;
					}
					//zoomFactor = this.lastZoomValue * preferChange;
					*/
					zoomFactor = preferChange;
					this.txtZoom.text = (zoomFactor * 100 ).toString();
					this.OnZoomEnterPress();
					//this.AdjustImageWRTCenter();
				}catch(err:Error){
					
					Logger.LogE(err);
				}
			}
			
			private function OnFitWidth():void{
				try{
					var zoomFactor:Number = this.lastZoomValue * (this.MoverStage.width - 36)/ this.ctrls[this.currentPage-1].width;
					this.txtZoom.text = (zoomFactor * 100 ).toString();
					this.OnZoomEnterPress();
					//this.AdjustImageWRTCenter();
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnOriginalSize():void{
				try{
					var zoomFactor:Number = 1;
					this.txtZoom.text = (zoomFactor * 100 ).toString();
					this.OnZoomEnterPress();
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnPageUp():void{
				try{
					
					if(this.selectedMark){
						this.selectedMark.Selected = false;
						this.selectedMark = null;
					} 
					if ( this.currentPage > 1 ){
						this.txtPageNo.text = String(this.currentPage-1);
						this.OnGotoPage();
						//this.OnFitPage();
						 
					}
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnPageDown():void{
				try{
					
					if(this.selectedMark){
						this.selectedMark.Selected = false;
						this.selectedMark = null;
					} 
					if ( this.currentPage < this.totalImages ){
						this.txtPageNo.text = String(this.currentPage+1);
						this.OnGotoPage();
						//this.OnFitPage(); 
					}
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnRedo():void {
				try {
					
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnUndo():void{
				try{
					//Alert.show(this.History.toString(),"History");
					this.undoFlag = true;
					if ( this.History.length == 0 ){
						return;
					}
					var lastAction:HistoryObject = this.History[this.History.length-1];
					
					if ( lastAction.PageNumber != this.currentPage ){
						this.txtPageNo.text = lastAction.PageNumber.toString();
						this.OnGotoPage();
						return;
					}
					
					var success:Boolean = true;
					
					switch(lastAction.Action.toUpperCase()){
						case "ZOOM":
							var zoomVal:Number = Number(lastAction.Properties.zoom.toString());
							this.txtZoom.text = (zoomVal * 100 ).toString();
							this.OnZoomEnterPress();
						break;
						case "GOTOPAGE":
							var pageNumber:Number = Number(lastAction.Properties.pageno.toString());
							this.txtPageNo.text = pageNumber.toString();
							this.OnGotoPage();
						break;
						case "REMOVEMARK":
							var markName:String = lastAction.Properties.MarkName.toString();
							this.ctrls[this.currentPage-1].RemoveMark(markName);
						break;
						case "ADDMARK":
							var markN:String = lastAction.Properties.marN.toString();
							var x:Number = Number(lastAction.Properties.marX.toString());
							var y:Number = Number(lastAction.Properties.marY.toString());
							var w:Number = Number(lastAction.Properties.marW.toString());
							var h:Number = Number(lastAction.Properties.marH.toString());
							var a:Number = Number(lastAction.Properties.marA.toString());
							var c:Number = Number(lastAction.Properties.marC.toString());
							OnAddMark(markN,x,y,w,h,a,c);
						 break;
						case "SETMARK":
							var markNm:String = lastAction.Properties.markName;
							var markX:Number = Number(lastAction.Properties.markX.toString());
							var markY:Number = Number(lastAction.Properties.markY.toString());
							var markW:Number = Number(lastAction.Properties.markW.toString());
							var markH:Number = Number(lastAction.Properties.markH.toString());
							var markA:Number = Number(lastAction.Properties.markA.toString());
							var markC:Number = Number(lastAction.Properties.markC.toString());
						
							this.ctrls[this.currentPage-1].SetMark(markNm,markX,markY,markW,markH,markA,markC);
							//OnAddMark(markNm,markX,markY,markW,markH,markA,markC); 
						break;
						case "COMMITCHANGE":
							var chkName:String = lastAction.Properties.chkName.toString();
							if ( chkName != "" ){
								var chk:CheckBox = this.vboxCommitGlobally.getChildByName(chkName) as CheckBox;
								if ( chk != null ){
									var Val:String = lastAction.Properties.chkValue.toString();
									if ( Val.toUpperCase() == "TRUE" ){
										chk.selected = true;
									}else{
										chk.selected = false;
									}
									// Added for bug fix
									this.commitStatus[this.currentPage-1] = chk.selected;
									this.ctrls[this.currentPage-1].CommitFlag = chk.selected;
								}
							}
						break;
					  
						case "ANTICLOCK":
							var antiClock:String = lastAction.Properties.RotationClockWise;
							
							//Alert.show(antiClock.toString());
							
							this.ctrls[this.currentPage-1].Rotation -= Number(antiClock);
							this.MakeRotationAdjustments(this.ctrls[this.currentPage-1]);
							//this.ctrls[this.currentPage-1].rotation ;
					    
					    break;
						case "CLOCKWISE":
							var clockWise:String = lastAction.Properties.RotationAntiClockWise;
							
							//Alert.show(antiClock.toString());
							
							this.ctrls[this.currentPage-1].Rotation += Number(clockWise);
							this.MakeRotationAdjustments(this.ctrls[this.currentPage-1]);
							//this.ctrls[this.currentPage-1].rotation ;
					    
					    break;
					    /////////// shahid code for undo Alpha
						case "UNDOALPHA":
							var undoAlpha:Number  = Number(lastAction.Properties.UndoAlpha);
							//Alert.show(undoAlpha.toString());
							//this.ctrls[this.currentPage-1].setAlpha(undoAlpha);
							this.selectedMark.alpha = undoAlpha;
							this.sliderAlpha.value = undoAlpha*100;
						
						break;

						case "REMOVECOMMENT":
							var undoComment:String  = String(lastAction.Properties.CommentName);
							//Alert.show(undoComment.toString());
							MainObject(this.ctrls[this.currentPage-1]).CommentController.RemoveCommentbyName(undoComment);
						
							//this.OnAlphaSliderChange();
						
						break; 
						
						case "ADDCOMMENTOBJECT":
							 var redoComment:XMLList  = XMLList(lastAction.Properties);
							//Alert.show(redoComment.commentobject.toXMLString()); 
							var xpos:Number = Number(redoComment.commentobject.@x);
							var ypos:Number = Number(redoComment.commentobject.@y);
							var UserName:String = redoComment.commentobject.@username.toString();
							var CommentName:String = redoComment.commentobject.@commentname.toString();
							
							//Alert.show(redoComment.toString());
							//Alert.show(redoComment.commentobject.@commentname.toString());
							
							//Alert.show("name:" + CommentName);
							var dragbound:Rectangle = this.GetContainerRectangle();
							MainObject(this.ctrls[this.currentPage-1]).CommentController.AddComment(dragbound, CommentName, "message", xpos,ypos);
						break; 
						
						case "PREVIOUSCOLOR":
							var previouscolor:String  = lastAction.Properties.PreviousColor;
							this.selectedMark.setStyle("backgroundColor",previouscolor); 
							this.ChangeStatus();
						break;  
						
						default:
							Alert.show("INVALID ACTION");
							success = false;
						break;
					}
					
					if ( success ){
						this.History.pop();
					}
				}catch(err:Error){
					Logger.LogE(err);
				}finally{
						this.undoFlag = false;					
				}
			}
			
			public function OnAddComment():void {
				try{
					var cX:Number = this.MoverStage.horizontalScrollPosition/this.lastZoomValue;
					var cY:Number = this.MoverStage.verticalScrollPosition/this.lastZoomValue;
					var dragBound:Rectangle = this.GetContainerRectangle();
					cX = 20;
					cY = 10;
					var commentName:String = MainObject(this.ctrls[this.currentPage-1]).CommentController.AddComment(dragBound, "","Add text here", cX, cY);
					//Alert.show(commentName.toString());
					 if ( commentName != "" ){
						var props:XML = new XML(<root />);
						props.CommentName = commentName;
						this.RecordHistory("REMOVECOMMENT",props); 
						}
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnSavePage():void{
				try{
					var tmpRes:XML = new XML('<images startuppage="'+this.currentPage.toString()+'"/>');
					var index:Number = 1;
					for each ( var mo:MainObject in this.ctrls ){
						var getData:XML = mo.getXMLData();
						if ( getData != null ){
							var chkselected:Boolean = this.commitStatus[index-1];
							if ( chkselected ){
								getData.CommitGlobally = "1"; 
							}else{
								getData.CommitGlobally = "0"; 								
							}
							tmpRes.appendChild(getData);
						}
						index ++;
					}
					//Alert.show(tmpRes.toXMLString(),"Saved Data");
					
					var uVars:URLVariables = new URLVariables();
					var reqs:URLRequest = new URLRequest();
					var transform:String = tmpRes.toXMLString();
					
					//Alert.show(transform); 
					
					transform = transform.split("<").join("@#@");
					transform = transform.split(">").join("$*$");
					
					
					
					uVars.dataToSave = transform;//tmpRes.toXMLString();
					uVars.documentID = this.fileToLoad;
					//reqs.url = "http://localhost:1685/FlexImageViewer/SaveData.aspx";
					reqs.url = "SaveData.aspx";
					reqs.method = URLRequestMethod.POST;
					reqs.data = uVars;
					
					//navigateToURL(reqs, "_blank");
					var ldr:URLLoader = new URLLoader();
					ldr.addEventListener(Event.COMPLETE,OnUpdateCompleteHandler);
					ldr.addEventListener(IOErrorEvent.IO_ERROR,OnIOError);
					ldr.load(reqs);
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function OnIOError(evt:IOErrorEvent):void{
				Alert.show(evt.text);
			}
			
			private function OnUpdateCompleteHandler(evt:Event):void{
				try{
					Alert.show(evt.target.data,"Returned");
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			private function MakeRotationAdjustments(parm:MainObject):void{
				try{
					var angleToTest:Number =parm.rotation%360;

					switch(angleToTest){
						case 0:
							parm.x = (this.MoverStage.width - parm.width ) / 2;
							parm.y = 0;
						break;
						case 90:
							parm.x = (this.MoverStage.width - parm.height ) / 2 + parm.height;
							parm.y = 0;
						break;
						case 180:
						case -180:
							parm.x = (this.MoverStage.width - parm.width ) / 2 + parm.width;
							parm.y = parm.height;
						break;
						case -90:
							parm.x = (this.MoverStage.width - parm.height ) / 2;
							parm.y = parm.width;
						break;
						default:
						break;
					}
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			public function MainCanvas_OnMouseOver(evt:MouseEvent):void{
				try{
					//Alert.show(evt.target.toString());
					if ( this.selectedMark != null )
						if ( this.selectedMark.isDragging || this.selectedMark.resizeFlag )
							this.selectedMark.OnMouseUp(evt);
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			/* public function PrintInfo(parm:String):void{
				try{
					this.txtInfo.text = parm;
				}catch(err:Error){
					Alert.show(err.toString()+"["+err.getStackTrace()+"]","Exception");
				}
			}  */

			private function OnClockWise():void{
				try{
					this.ctrls[this.currentPage-1].Rotation += 90;
					var rotationClockWise:String = "90";
					if ( rotationClockWise){
						var props:XML = new XML(<root />);
						props.RotationClockWise = rotationClockWise;
						this.RecordHistory("ANTICLOCK",props);
						//Alert.show(props.toString());					
					}
					this.MakeRotationAdjustments(this.ctrls[this.currentPage-1]);
				}catch(err:Error){
					Logger.LogE(err);
				}
			}

			private function OnAntiClockWise():void{
				try{
					this.ctrls[this.currentPage-1].Rotation -= 90;
					
					var rotationAntiClockWise:String = "90";
					if ( rotationAntiClockWise){
						var props:XML = new XML(<root />);
						props.RotationAntiClockWise = rotationAntiClockWise;
						this.RecordHistory("CLOCKWISE",props);
						//Alert.show(props.toString());					
					}
					this.MakeRotationAdjustments(this.ctrls[this.currentPage-1]);
				}catch(err:Error){
					Logger.LogE(err);
				}
			}

			private function cpChangeEvt(event:ColorPickerEvent):void {
				try {
					if(this.selectedMark != null){						 
						var selectedcolor:String = this.selectedMark.getStyle("backgroundColor");
						if ( selectedcolor){
						var props:XML = new XML(<root />);
						props.PreviousColor = selectedcolor;
						this.RecordHistory("PREVIOUSCOLOR",props);
						//Alert.show(props.toString());					
					}
						//Alert.show(selectedcolor.toString());
						this.selectedMark.setStyle("backgroundColor",cp.selectedColor);					
					}
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
// Rotation undo is problem - done
// del undo is problem - done
// multiple selected marks with border/selection at creation time - done
// require consistant comment size - done
// fit page is problem - done
// beyond stage mouse up - done
// draging image is available - done
// del keyboard button functionality is required - done
// key board events are not working properly - pending
// Comment is draging while try to scroll
